import React, { useEffect } from "react";
import { Link, Routes, Route, useParams } from "react-router-dom";

const Home = () => {
  return <div>这是home页面</div>;
};
const About = () => {
  return <div>这是about页面</div>;
};
const Detail = () => {
  // useParams可以直接可以获取到动态路由参数的对象
  const params = useParams();
  useEffect(() => {
    console.log(params);
  }, [params]);

  return <div>这是Detail页面</div>;
};

const App = () => {
  return (
    <>
      <h2>react动态路由参数</h2>

      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail/1234">详情页1</Link>
        </li>
        <li>
          <Link to="/detail/5678">详情页2</Link>
        </li>
      </ul>

      <Routes>
        {/* 动态路由的方式和vue一样，也是在path加/:id */}
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="/detail/:id" element={<Detail />}></Route>
      </Routes>
    </>
  );
};

export default App;
